import { Component, OnInit, Input } from '@angular/core';
import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { AlertController } from '@ionic/angular';
import { EventService } from 'src/app/shared/service/event.service';
@Component({
  selector: 'app-designs-record-list',
  templateUrl: './designs-record-list.component.html',
  styleUrls: ['./designs-record-list.component.scss'],
})
export class DesignsRecordListComponent implements OnInit {
  @Input() tagData: any;
  pageNo = 1;
  constructor(
    public httpService: HttpService,
    public appGlobalService: AppGlobalService,
    public pageModalService: PageModalService,
    public alertController: AlertController,
    private eventService: EventService,
  ) { }

  ngOnInit() {

  }
  // 上滑加载
  loadMoreData(event) {
    this.pageNo = this.pageNo + 1;
    if (this.tagData.data.data.length >= this.tagData.data.pageTotal) {
      event.target.disabled = true;
      // this.appGlobalService.showToast("没有数据了...", 1000, "middle");
      // event.complete();
      return;
    }
    if (this.tagData.data.submit) {
      if (this.tagData.data.submit.content) {
        this.tagData.data.submit.content.pageNo = this.pageNo;
      }
      this.tagData.data.submit.sessionId = localStorage.getItem("sessionId");
    }
    this.httpService.post(
      "application/submit",
      this.tagData.data.submit,
      res => {
        // 请求数据成功
        if (res.success) {
          const resList = res.data.data;
          this.tagData.data.submit = res.data.submit;
          // 追加返回数据
          if (resList && resList.length > 0) {
            // 追加数据
            resList.map(item => {
              this.tagData.data.data.push(item);
            });
          } else {
            event.target.disabled = true;
          }
        } else {
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, "middle");
        }
        event.target.complete();
      },
      err => {
        // 请求数据失败
        this.appGlobalService.showToast(err.message, 2000, "middle");
        event.target.complete();
      }
    );
  }
  async btnClick(item) {
    if (item.link) {
      this.pageModalService.openPage(item.link);
    } else if (item.button && item.button.submit) {
      const alert = await this.alertController.create({
        message: item.button.submit.content.message ? item.button.submit.content.message : '是否删除?',
        buttons: [
          {
            text: "取消",
            role: "cancel",
            cssClass: "secondary"
          },
          {
            text: '确定',
            handler: () => {
              const params = {
                content: item.button.submit.content,
                id: item.button.submit.id,
                sessionId: localStorage.getItem("sessionId")
              };
              this.httpService.post("application/submit", params, res => {
                if (res.success) {
                  if (res.message && res.message.length > 0) {
                    this.appGlobalService.showToast(res.message, 1500, "middle");
                  }
                  // 刷新页面
                  setTimeout(() => {
                    this.eventService.eventEmit.emit("reloadCurrentPage", "reloadCurrentPage");
                  }, 200);
                }
              });
            }
          }
        ]
      });
      await alert.present();
    }
  }
}
